<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class purchaser_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		
	public function get_branch(){
	$q = $this->db->get('branch');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
		
	}
	

	public function get_purchaserList(){
			//$q = $this->db->get('users')->result();
			//print_r($q);exit;
	$this->db->select('users.*,branch.name AS branch_name');
	$this->db->from('users', 'branch','users_groups' );
	$this->db->join('branch', 'users.company= branch.id');
	$this->db->join('users_groups', 'users.id= users_groups.user_id AND users_groups.group_id= 3  '  );
	//$this->db->join('company', 'product.id_company= company.id');
	$result = $this->db->get();
	return $result;
		//echo "i am here";exit;
/*		
		$this->db->select('p.*');
		$this->db->from('product p, category cg, company cm');
	//	$this->db->where('product.group ='.$id);
		$this->db->join('category cg','p.id_category=cg.id');
		$this->db->join('company cm','p.id_company=cm.id');
		 $query = $this->db->get();
		echo "i am here";
		print_r($query->result());exit;*/
	
	
		//$query = $this->db->get('product');
	//eturn $query;

		
		
		} 

public function get_product($id){
	$query = $this->db->get_where('product', array('id' => $id));
	return $query;

	}

public function insert($data){

		$this->db->insert('salesman',$data);
	}

/*public function get_groups(){
	$this->db->select('acc_group.*,projects.name');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$id);
		$this->db->join('projects','tasks.project_id=projects.id');
		return $this->db->get()->row_array();
		$query = $this->db->get('acc_group');
	return $query;
}
*/
public function edit_salesman($id){
		$query = $this->db->get_where('salesman', array('id' => $id));
		return $query;
	}

public function update($edit_id, $data){
		$this->db->update('salesman',$data,array('id'=>$edit_id));
	}

public function delete($id){
		$this->db->delete('users',array('id'=>$id));
		$this->db->delete('users_groups',array('user_id'=>$id));		
	}
	
public function get(){
	   $str = $this->datatables
			->select("users.id as id, users.username,branch.name, users.phone, users.email,users.address")
			->from("users")
			->join('branch', 'users.company= branch.id')
			->join("users_groups", "users.id= users_groups.user_id AND users_groups.group_id= 3 " )
			->add_column("Actions", 
			"<center>			<a href='edit_purchaser/$1'>
								<i class='icon-edit'></i>
								</a>&nbsp;&nbsp;
							    <a href='delete_purchaser/$1' onClick=\"return confirm('". 'Do U Want to Delete Purchase' ."')\">
								<i class='icon-trash'></i>
								</a></center>", "id")
			->unset_column('id');
		
	   return  $this->datatables->generate();	
}		



}
